package com.personal.service.Impl;

import cn.hutool.Hutool;
import cn.hutool.core.util.DesensitizedUtil;
import com.github.pagehelper.PageHelper;
import com.personal.bo.AllQueryBO;
import com.personal.enums.HomeEnum;
import com.personal.exception.ExceptionUtils;
import com.personal.mapper.HomeMapper;
import com.personal.mapper.UsersMapper;
import com.personal.po.ResourcesPO;
import com.personal.pojo.Dynamic;
import com.personal.pojo.ResourcesImg;
import com.personal.service.HomeService;
import com.personal.utils.Result;
import com.personal.vo.QueryCatListVO;
import com.personal.vo.ResourcesVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class HomeServiceImpl implements HomeService {

    @Autowired
    private HomeMapper homeMapper;

    @Autowired
    private UsersMapper usersMapper;

    /**
     * 查询全站
     * @param allQueryBO
     * @return
     */
    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public List<ResourcesVO> allQuery(AllQueryBO allQueryBO) {
        log.info("查询全站接口");
        PageHelper.startPage(allQueryBO.getPageNum(), allQueryBO.getPageSize());
        // 这是最后的结果list
        List<ResourcesVO> resourcesVoList = new ArrayList<>();
        List<ResourcesPO> resourcesList = homeMapper.allQuery(allQueryBO);
        for (ResourcesPO resourcesPo : resourcesList) {
            ResourcesVO resourcesVO = new ResourcesVO();
            BeanUtils.copyProperties(resourcesPo, resourcesVO);

            //根据id查询发布人头像
            String face = usersMapper.getFaceById(resourcesPo.getUserId());
            resourcesVO.setFace(face);

            //查询图片
            List<ResourcesImg> imgList = homeMapper.queryAllImg(resourcesPo.getId());
            resourcesVO.setResourcesImgList(imgList);

            //查询资源文章的价格
            int priceNormal = homeMapper.queryResourcesPrice(resourcesPo.getId());
            String priceVip = homeMapper.queryResourcesPriceVip(resourcesPo.getId());
            resourcesVO.setPriceNormal(priceNormal);
            resourcesVO.setPriceVip(priceVip);

            resourcesVoList.add(resourcesVO);
        }
        return resourcesVoList;
    }

    /**
     * 查询分类列表
     * @return
     */
    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public Result queryCatList() {
        log.info("查询分类列表接口");
        List<QueryCatListVO> catList = homeMapper.queryCatList();
        if (catList.isEmpty()){
            ExceptionUtils.error(HomeEnum.QUERY_CAT_IS_NULL);
        }
        return Result.success(catList);
    }

    /**
     * 查询前200条数据动态列表
     * @return
     */
    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public Result queryDynamicList() {
        log.info("查询前200条动态列表接口");
        List<Dynamic> dynamicList = homeMapper.queryDynamicList();
        dynamicList.forEach(one -> {
            String nickname = one.getNickname();
            one.setNickname(DesensitizedUtil.chineseName(nickname));
        });
        return Result.success(dynamicList);
    }
}
